package com.socket.socket;

import org.junit.jupiter.api.Test;

import java.io.*;
import java.net.Socket;

import static org.junit.jupiter.api.Assertions.assertEquals;

class SocketServiceTest {

    private static final String SERVER_HOST = "localhost";
    private static final int SERVER_PORT = 7888;

    @Test
    void testSocketCommunication() throws IOException {
        // Connect to the server
        try (Socket clientSocket = new Socket(SERVER_HOST, SERVER_PORT);
             OutputStream outputStream = clientSocket.getOutputStream();
             PrintWriter writer = new PrintWriter(outputStream, true);
             InputStream inputStream = clientSocket.getInputStream();
             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {

            // Send a message to the server
            String message = "Hello, Server!";
            writer.println(message);

            // Read the response from the server
            String response = reader.readLine();
            assertEquals("Echo: " + message, response);

            System.out.println("Server response: " + response);
        }
    }

    @Test
    void testSocketDisconnect() throws IOException {
        // Connect to the server
        try (Socket clientSocket = new Socket(SERVER_HOST, SERVER_PORT)) {
            System.out.println("Connected to server successfully.");
        }
        System.out.println("Client disconnected successfully.");
    }
}
